package org.rkuk.kasutil.test.system.handler 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import org.rkuk.kasutil.designPatternTools.AbstractTool;
	import org.rkuk.kasutil.test.event.HandlerEvent;
	import org.rkuk.kasutil.test.framework.TestMethod;
	/**
	 * ...
	 * @author rkuk
	 */
	public class Handler extends EventDispatcher
	{
		private var eventTarget_:EventDispatcher;
		private var eventType_:String;
		private var eventMethod_:TestMethod;
		private var triggerEvent_:Event;
		
		public function Handler(eventTarget:EventDispatcher, eventType:String, eventMethod:TestMethod)
		{
			AbstractTool.makeAbstractClass(this, Handler);
			eventTarget_ = eventTarget;
			eventType_ = eventType_;
			eventMethod_ = eventMethod;
		}
		
		protected function onTargetEvent(e:Event):void 
		{
			stop();
		}
		
		protected function notifyHandlerEvent(triggerEvent:Event, handlerEventType:String = HandlerEvent.HANDLER_TRIGGER):void
		{
			triggerEvent_ = triggerEvent;
			dispatchEvent(new HandlerEvent(handlerEventType));
		}
		
		public function start():void
		{
			eventTarget_.addEventListener(eventType_, onTargetEvent);
		}
		
		public function stop():void
		{
			eventTarget_.removeEventListener(eventType_, onTargetEvent);
			eventTarget_ = null;
		}
		
		public function get eventMethod():TestMethod 
		{
			return eventMethod_;
		}
		
		public function get triggerEvent():Event 
		{
			return triggerEvent_;
		}
	}
}